package BookManage;

import com.mysql.cj.MysqlConnection;
import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class insertBook {
    public static void main(String[] args) {
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            connection=operation.My_Get_Connection();
            String sql="insert into borrow_info(book_id,student_id,"+
                    "start_time,end_time) select b.id,s.id,?,?"+
                    "from book b,student s where b.name=? and s.name=?";
            statement=connection.prepareStatement(sql);
            statement.setTimestamp(1,operation.getTimestamp("2019-09-25 17:50:00"));
            statement.setTimestamp(2,operation.getTimestamp("2019-10-20 17:50:00"));
            statement.setString(3,"诗经");
            statement.setString(4,"貂蝉");
            int result=statement.executeUpdate();
            if(result!=1){
                System.out.println("插入失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            operation.My_close(connection,statement,null);
        }
    }
}
